<template>
  <div class="nodeflow-components">
    <div class="node-wrap">
      <addNode @childNodeP="childNodeP" />
      <div v-for="(item, index) in approver" :key="index" class="node-wrap-box">
        <div class="node_wrap_conter">
          <div class="title">
            <span class="editable-title">审核人</span>
            <i class="el-icon-close close" @click="closeRemove(index)" />
          </div>
          <div class="content" @click="audit(item)">
            <div class="text">
              <span class="placeholder">{{ item.nodeName }}</span>
            </div>
            <i class="el-icon-arrow-right arrow" />
          </div>
        </div>
        <addNode @childNodeP="childNodeP" />
      </div>
    </div>
    <el-drawer title="审批人设置" :visible.sync="drawer" :modal="false">
      <el-form ref="form" v-model="ruleForm">
        <div class="drawer_box">
          <el-form-item label="">
            <el-radio-group v-model="ruleForm.utype">
              <el-radio :label="1">指定成员</el-radio>
              <el-radio :label="2">直属上级</el-radio>
            </el-radio-group>
            <div v-if="ruleForm.utype == 1">
              <div class="flex mt15">
                <el-select
                  v-model="user_id"
                  placeholder="请选择"
                  size="small"
                  value-key="id"
                  clearable
                  filterable
                  @change="changeItem"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.id"
                    :label="item.realname"
                    :value="item"
                  />
                </el-select>
              </div>
              <div class="member_item">
                <el-tag
                  v-for="tag in ruleForm.user"
                  :key="tag.realname"
                  closable
                  size="small"
                  class="ml10"
                  :type="tag.type"
                  @close="handleClose(tag)"
                >
                  {{ tag.realname }}
                </el-tag>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="多人审批方式" class="w300">
            <el-radio-group v-model="ruleForm.attr">
              <el-radio :label="1">或签（一名成员同意即可）</el-radio>
              <el-radio
                :label="2"
                class="mt10"
              >会签（需所有成员统计）</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
      </el-form>
      <el-button
        class="ml10"
        size="small"
        type="primary"
        @click="determine"
      >确定</el-button>
    </el-drawer>
  </div>
</template>

<script>
class RuleForm {
  utype = 1; // 1 = 固定审批人 2 = 直属上级
  attr = 1; // 1 = 或签 2 = 会签
  nodeName = '申请人自选';
  user = [
    // 阶段审批人员信息【数组】
  ];
}
class ApproverList {
  approver = [
    {
      utype: 1, // 1 = 固定审批人 2 = 直属上级
      attr: 1, //	1 = 或签 2 = 会签
      nodeName: '申请人自选',
      user: [
        // 阶段审批人员信息【数组】
      ]
    }
  ]; // 审批人
}
import addNode from './addNode.vue';
import { qywxall } from '@/api/qywx';
export default {
  name: 'NodeWrap',
  components: {
    addNode
  },
  props: {
    approver: {
      type: Array,
      default: () => new ApproverList()
    }
  },
  data() {
    return {
      drawer: false,
      user_id: '',
      ruleForm: new RuleForm(),
      userList: [] // 用户列表
      // approver: []
    };
  },
  mounted() {
    this.userlist();
  },
  methods: {
    initApprover(approver) {
      console.log(approver);
      let real_name = [];
    //  approver.map((item, index) => {
    //     if (item.utype == 1) {
    //       item.user.map((val) => {
    //         real_name.push(val.realname);
    //         item.nodeName = `指定成员(${real_name})`;
    //       });
    //     } else if (item.utype == 2) {
    //       item.nodeName = '申请人直属上级';
    //     } else if (item.utype == '') {
    //       item.nodeName = '申请人自选';
    //     }
    //   });
    },
    audit(item) {
      this.ruleForm = item;
      this.drawer = true;
    },
    // 弹框确定
    determine() {
      if (this.ruleForm.utype == 1 && this.ruleForm.user.length <= 0) {
        return this.$notify({
          title: '警告',
          message: '请选择指定成员',
          type: 'warning'
        });
      }
      if (this.ruleForm.utype == 1) {
        let realname = [];
        this.ruleForm.user.map((item) => {
          realname.push(item.realname);
        });
        this.ruleForm.nodeName = `指定成员(${realname})`;
      } else if (this.ruleForm.utype == 2) {
        this.ruleForm.nodeName = '申请人直属上级';
      } else if (this.ruleForm.utype == '') {
        this.ruleForm.nodeName = '申请人自选';
      }

      this.drawer = false;
    },
    // 删除审核人
    closeRemove(index) {
      this.approver.splice(index, 1);
    },
    // 添加空的审批人
    childNodeP(appUser) {
      this.approver.push({ ...appUser });
    },
    // 添加用户成员到集合里
    changeItem(val) {
      if (this.ruleForm.user.findIndex((item) => item.id == val.id) == -1) {
        this.ruleForm.user.push(val);
        this.user_id = '';
      } else {
        this.$message({
          message: '已添加该审批人',
          type: 'warning'
        });
      }
    },
    handleClose(tag) {
      this.ruleForm.user.splice(this.ruleForm.user.indexOf(tag), 1);
    },

    async userlist() {
      const data = await qywxall();
      this.userList = data;
    }
  }
};
</script>

<style lang="scss">
.node-wrap-box {
  .node_wrap_conter {
    border-radius: 15px;
    overflow: hidden;
    box-shadow: 2px 2px 2px 1px #efefef;
  }
  .title {
    display: flex;
    justify-content: space-between;
    padding: 0 15px;
    height: 30px;
    align-items: center;
    background: rgba(250, 205, 145, 1);
    color: #b8741a;
  }
  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 15px;
    font-size: 14px;
    line-height: 40px;
  }
}
.drawer_box {
  padding: 0 15px;
}
.member_item {
  width: 300px;
  height: 100px;
  border: 1px solid rgba(121, 121, 121, 1);
  margin-top: 15px;
  padding: 10px 5px;
  display: flex;
  flex-wrap: wrap;
  .item_name {
    display: inline-block;
    padding: 0 5px;
    border-radius: 6px;
    height: 30px;
    line-height: 30px;
    border: 1px solid rgba(121, 121, 121, 1);
    flex-shrink: 0;
    margin-left: 10px;
    position: relative;
  }
  .icon_position {
    position: absolute;
    left: 57px;
    font-size: 20px;
    top: 162px;
  }
}

.dingflow-design {
  .box-scale {
    display: inline-block;
    left: 40%;
    .node-wrap-box {
      display: inline-flex;
      flex-direction: column;
      position: relative;
      width: 280px;
      min-height: 72px;
      background: #fff;
      border-radius: 4px;
      cursor: pointer;
    }
  }
}

.dingflow-design .add-node-btn {
  user-select: none;
  width: 240px;
  padding: 30px 0;
  display: flex;
  -webkit-box-pack: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-box-flex: 1;
  flex-grow: 1;
}

.dingflow-design .node-wrap {
  flex-direction: column;
  -webkit-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  // padding: 0 50px;
  position: relative;
}

.dingflow-design .branch-wrap,
.dingflow-design .node-wrap {
  display: inline-flex;
  // width: 100%;
}

.dingflow-design .add-node-btn .btn {
  outline: none;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
  width: 30px;
  height: 30px;
  background: #4880ff;
  border-radius: 50%;
  position: relative;
  border: none;
  line-height: 30px;
  -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  // margin-left: 36px;
}

.dingflow-design .add-node-btn .btn:hover {
  transform: scale(1.3);
  box-shadow: 0 13px 27px 0 rgba(0, 0, 0, 0.1);
}

.dingflow-design .add-node-btn .btn:active {
  transform: none;
  background: #1e83e9;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
}

//---------------------------//
.start-node.node-wrap-box .content .text {
  display: block;
  white-space: nowrap;
}
</style>
